package com.lft.examsys.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lft.examsys.entity.Item;
import com.lft.examsys.mapper.ItemMapper;
import com.lft.examsys.mapper.PaperMapper;
import com.lft.examsys.model.IModel;
import com.lft.examsys.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @author ：Qingchen
 * @date ：Created in 2022/3/30 16:09
 * @description：题目服务层
 * @modified By：
 * @version: $
 */
@Service
public class ItemService {
    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private PaperMapper paperMapper;

    public Page<Item> getAllByPage(IModel model, Integer page, Integer size, String elName) {
        return PageUtil.getAllByPage(model, page, size, itemMapper, elName);
    }


    public Item getItem(Integer id) {
        return itemMapper.selectById(id);
    }

    public Integer saveItem(Item item) {return itemMapper.updateById(item);}

    public Integer deleteItem(Integer id) {
        paperMapper.deleteItem(id);
        return itemMapper.deleteById(id);
    }

    public Integer addItem(Item item) {
        return itemMapper.insert(item);
    }

    public List<Item> getItemByPaperId(Integer id) {
        return itemMapper.selectByPaperId(id);
    }

    // TEST: add values
    public Boolean test(String title, String content, String answer, String type_id) {return itemMapper.insertValues(title,content, answer, type_id);}

    public List<Item> randomItems(Integer limitation) {
        return itemMapper.randomItems(limitation);
    }

    public Boolean checkAnswer(Integer id, List<String> list) {
        Collections.sort(list);
        StringBuilder answer = new StringBuilder();
        for (String s : list) {
            answer.append(s);
        }
        String trueAnswer = itemMapper.selectAnswerById(id);
        return answer.toString().equals(trueAnswer);
    }

    public Page<Item> getAllByPaperId(Integer id, Page<Item> pageInfo) {
        return itemMapper.getAllByPaperId(pageInfo, id);
    }

}
